package com.example.demo.config.artemis;

import org.apache.activemq.artemis.api.core.QueueConfiguration;
import org.apache.activemq.artemis.api.core.client.*;

public class MqClient {

    public static void main(String[] args) {
        try {
            ServerLocator serverLocator = ActiveMQClient.createServerLocator("vm://0");
            ClientSessionFactory factory = serverLocator.createSessionFactory();

            ClientSession session = factory.createSession();
            session.addMetaData(ClientSession.JMS_SESSION_IDENTIFIER_PROPERTY, "jms-client-id");
            session.addMetaData("jms-client-id", "my-session");
            session.createQueue(new QueueConfiguration("example"));
            ClientProducer producer = session.createProducer("example");
            ClientMessage message = session.createMessage(true);
            message.getBodyBuffer().writeString("Hello......................");
            producer.send(message);
            session.start();
            ClientConsumer consumer = session.createConsumer("example");
            ClientMessage msgReceived = consumer.receive();
            System.out.println("Message from server: " + msgReceived.getBodyBuffer().readString());
            session.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
